14. Independent Research

Independent Research

Question:

Start Quiz:

Solution:

INSTRUCTOR NOTE:

In case you're interested, here's a Short History of JavaScript.



Please wait until after you've completed the quiz to read the following.



Anonymous functions are functions that don't have a name and are often returned by other functions and objects.


Some JavaScript libraries ask for a callback function to be executed once they have have the results of a task. Anonymous functions are used in these cases because there is not a need to call the function by name outside the confines of the enclosing function.


For example, the code below reads a JSON file from the server. After loading, it executes an anonymous function to print out the data.

$.getJSON("test.json", function(data) {
    console.log(data);
});


Anything that uses an anonymous function could also use a named function. The following code is also valid and is equivalent to what's listed above:

var printData = function(data){
  console.log(data)
};
$.getJSON("test.json", printData);



Anonymous functions in JavaScript



Follow your instructors!

@cwpittman

+jameswilliams